<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="1" :labelWidth="110" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { addAddItem, editAddItem } from '@/api/repair/repair/addItem';

export default {
  name: 'OrderModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'edit'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      console.log(this.type);
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.addItemCode;
        console.log(this.rowData);
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '附加项目代码',
          fieldName: 'addItemCode',
          placeholder: '请输入...',
          rules: [
            { required: true, message: '请输入附加项目代码', trigger: 'blur' },
            { min: 8, max: 8, message: '请输入8个字符!', trigger: 'blur' }
          ],
          disabled: this.type === 'edit' ? true : false
        },
        {
          type: 'INPUT',
          label: '附加项目名称',
          fieldName: 'addItemName',
          placeholder: '请输入...',
          rules: [
            { required: true, message: '请输入附加项目名称', trigger: 'blur' },
            { min: 0, max: 60, message: '最多输入60个字符!', trigger: 'blur' }
          ]
        },
        {
          type: 'INPUT',
          label: '附加项目费用',
          unit: '元',
          initialValue: 0,
          fieldName: 'addItemAmount',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入附加项目费用', trigger: 'blur' }]
        },

        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'downTag',
          placeholder: '请选择',
          itemList: this.createDictList(1045),
          initialValue: 10451001,
          disabled: true
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: this.createDictList(1004),
          initialValue: 10041001
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      console.log(this.type);
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          console.log(this.type);
          res = await addAddItem({ dto: this.saveData });
        }
        if (this.type === 'edit') {
          res = await editAddItem(this.saveData, this.recordId);
        }

        // if (res.resultCode === 200) {
        this.closeDialog('refresh');
        //  }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped></style>
